// 这里把 index.js 也作为一个模块去定义并加载，这样才其他地方可以通过 require('./index') 来加载它，如果用 seajs.use 的话就不行
define(function(require, exports, module){

  var $ = require('../lib/jquery');  // jquery模块
  var bs = require('../lib/bootstrap-3.0.0-dist/dist/js/bootstrap');  // bootstrap模块
  var _ = require('../main');  // main模块
  var commonurl=_.getUrl();
  var ticket = _.getTicket();
  var userName = _.getUserIndex();
  var page=0;

  //ajax数据交互
  loadingData(0,5,'');
  dropdownData();
  staticData();
  getUserInfo();
  $("#modelChooerName").val("模块名称");
  $("#modelChooerKey").val("key");
  //点击新增用户，弹出modal
  $(".add").on("click",function(){
    // confirm("提示", "保存成功");
  	 $("#sqlConfigName").val(_.formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss'));
  });

    //保存新建配置里面的数据
    $(".addSave").on("click",function(){
      var sqlConfigName= $("#sqlConfigName").val();
      var sqlAllconfig= $("#sqlAllconfig").val();
      var sqlConfigKey= $("#sqlConfigKey").val();
      var sqlConfigValue= $("#sqlConfigValue").val();
      var sqlConfigRemark= $("#sqlConfigRemark").val();
      var sqlConfigModel= $("#sqlConfigModel").val();
      var consult= $("#consult").val();
      var sqlFlag=$("#sqlFlag").is(':checked');
      if (sqlConfigName!=null&&sqlAllconfig!=null&&sqlConfigKey!=null&&sqlConfigValue!=null&&sqlConfigRemark!=null&&sqlConfigModel!=null&&consult!=null) {
      $.ajax({ 
        url: commonurl+"nginx/postConfSql",
        type: 'POST',
        headers: {"Authorization":ticket},  
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({"name":sqlConfigName,"key":sqlConfigKey,"value":sqlConfigValue,"flag":sqlFlag,"remark": sqlConfigRemark,"allConf":sqlAllconfig,"consult":consult,"model":sqlConfigModel}), 
        success: function(json){
           confirm("提示", "保存成功");
          },
        error:function(json){
            confirm("提示","保存失败，检查网络");
          }
        });
      }else{
        confirm("提示","所有值不能为空");
      }
      if ($("#sqlFlag").is(':checked')==true) {
        $.ajax({ 
          url: commonurl+"nginx/putConf?address="+filePath,
          type: 'PUT',
          headers: {"Authorization":ticket},  
          contentType: 'application/json; charset=utf-8',
          data: JSON.stringify({"name":sqlConfigName,"key":sqlConfigKey,"value":sqlConfigValue,"flag":sqlFlag,"remark": sqlConfigRemark,"allConf":sqlAllconfig,"consult":consult,"model":sqlConfigModel}), 
          success: function(json){
                confirm("提示","装配成功");
           },
          error:function(json){
                confirm("提示","装配失败");
           }
         });
      }
      // alert(sqlConfigValue+sqlAllconfig+sqlConfigKey+sqlConfigValue+sqlConfigRemark+sqlFlag+$("#sqlFlag").is(':checked'));
  });
  //向下翻页按钮
    $("#modelNext").on("click",function(){
      var modelKey='';
      if ($("#modelChooerKey").val()!='key') {
          modelKey=$("#modelChooerKey").val()
      }
      page++;
      var templates="<span><a style='width:20px'>"+page+"</a></span>";
      $(".navigation ul li:eq(1)").empty("");
       $(".navigation ul li:eq(1)").append(templates);
       $(".form_table table  tr:not(:first)").empty(""); 
      loadingData(page,5,modelKey);
  });
  //向上翻页按钮点击事件
    $("#modelPrevious").on("click",function(){
      if (page==0) {
        confirm("提示","已经是第一页了");
      }else{
         page--;
      var templates="<span><a style='width:20px'>"+page+"</a></span>";
      $(".navigation ul li:eq(1)").empty("");
       $(".navigation ul li:eq(1)").append(templates);
       $(".form_table table  tr:not(:first)").empty(""); 
      loadingData(page,5,'');
      }
  }); 

  //点击删除数据库配置项 
    $(".deleSql").on("click",function(){
    confirm("提示", "保存成功");
    var conf_id=$("#conf_id").val();
    $.ajax({ 
    url: commonurl+"nginx/deleConfSql?id="+conf_id+"&address="+filePath,
    type: 'DELETE',
    headers: {"Authorization":ticket},  
    contentType: 'application/json; charset=utf-8',
    success: function(json){
        confirm("提示", "删除成功");
      },
    error:function(json){
            confirm("提示","删除失败");
           }
    });    
  });
  //点击保存用户
  $(".save").on("click",function(){
    var remark=$("#remark").val();
    var conf_id=$("#conf_id").val();
    var conf_name= $("#conf_name").val();
    var conf_key= $("#conf_key").val();
    var conf_value= $("#conf_value").val();
    var conf_flag= $("#conf_flag").val();
    $.ajax({ 
    url: commonurl+"nginx/putConf?address="+filePath,
    type: 'PUT',
    headers: {"Authorization":ticket},  
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({"id":conf_id,"name":conf_name,"key":conf_key,"value":conf_value,"flag":conf_flag,"remark": remark,"allConf":""}), 
    success: function(json){
        confirm("提示", "保存成功");
      },
    error:function(json){
            confirm("提示","保存失败，检查网络");
           }
    });
  });


//取消配置项在文件的装配
  $("#quxiaoPeizhi").on("click",function(){
    var urlType=commonurl+"nginx/addConf?address="+filePath;
    var httpType='POST'  
    if ($("#quxiaoPeizhi").text()=="取消配置") {
      urlType=commonurl+"nginx/delConf?address="+filePath;
      httpType='DELETE'
    }
    var remark=$("#remark").val();
    var conf_id=$("#conf_id").val();
    var conf_name= $("#conf_name").val();
    var conf_key= $("#conf_key").val();
    var conf_value= $("#conf_value").val();
    var conf_flag= $("#conf_flag").val();
    $.ajax({ 
    url: urlType,
    type: httpType,
    headers: {"Authorization":ticket}, 
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({"id":conf_id,"name":conf_name,"key":conf_key,"value":conf_value,"flag":conf_flag,"remark": remark,"allConf":""}), 
    success: function(json){
        confirm("提示", "保存成功"+conf_name+"="+conf_value);
        setTimeout(function(){  //使用  setTimeout（）方法设定定时1000毫秒
                               window.location.reload();//页面刷新
                    },1000);
      },
    error:function(json,XMLHttpRequest, textStatus, errorThrown){
            confirm("提示","保存失败，检查网络"+urlType+"="+httpType);
            setTimeout(function(){  //使用  setTimeout（）方法设定定时1000毫秒
                               window.location.reload();//页面刷新
                    },1000);
           }
    });
  })
  function loadingData(page,size,model){
    var urlType;
    if (model=='') {
        urlType=commonurl+"nginx/getConfSql?page="+page+"&size="+size;
    }else{
        urlType=commonurl+"nginx/getConfSqlByModel?page="+page+"&size="+size+"&model="+model;
    }
    //ajax数据交互 
   $.ajax({ 
    url: urlType,
    type: 'GET', 
    headers: {"Authorization":ticket}, 
    success: function(json){
        var admins_list=json;
        // var dropdown1=json.dropdown1;
        // var dropdown2=json.dropdown2;
        if (admins_list==''&&page!=0) {
          confirm("提示","没有数据了");
          page--;
          var templates="<span><a style='width:20px'>"+page+"</a></span>";
          $(".navigation ul li:eq(1)").empty("");
          $(".navigation ul li:eq(1)").append(templates);
          $(".form_table table  tr:not(:first)").empty("");
          var modelKey='';
          if ($("#modelChooerKey").val()!='key') {
          modelKey=$("#modelChooerKey").val()
          } 
          loadingData(page,5,modelKey);
        }
        //遍历表格
        $.each(admins_list, function(commentIndex, comment){
            var templates="<tr><td>"+admins_list[commentIndex].id+"</td><td>"+admins_list[commentIndex].name+"</td><td>"+admins_list[commentIndex].key+"</td><td>"+admins_list[commentIndex].value+"</td><td>"+admins_list[commentIndex].flag+"</td></tr>"
            $(".form_table table").append(templates);
            // $(".left_table table").on("click","tr",function(){
            $(".form_table table").on("click","tr",function(){
                 _.slideDown($(".table_detail"));
                 _.addActive($(this),$(this).siblings());
                  //拿到这一行的信息在表单展示
                 //var user=$(this).find("td").eq(1).html();
                  if($(this).find("td").eq(0).html()==admins_list[commentIndex].id){
                    $("#conf_id").val(admins_list[commentIndex].id);
                    $("#conf_name").val(admins_list[commentIndex].name);
                    $("#conf_key").val(admins_list[commentIndex].key);
                    $("#conf_value").val(admins_list[commentIndex].value);
                    $("#conf_flag").val(admins_list[commentIndex].flag);
                    $("#remark").val(admins_list[commentIndex].remark);
                    if (admins_list[commentIndex].flag) {
                      $("#quxiaoPeizhi").text("取消配置");
                    }else{
                      $("#quxiaoPeizhi").text("写入配置");
                    }
                  }
                });
         });

      }});

  }
  function dropdownData(){
    //ajax数据交互 
   $.ajax({ 
    url: commonurl+"nginx/getModelSql",
    type: 'GET',
    headers: {"Authorization":ticket},  
    success: function(json){
        var dropdown1=json;
        //遍历表格
        $.each(dropdown1, function(commentIndex, comment){
            var templates="<li><a id='choosemodel"+dropdown1[commentIndex].id+"'>"+dropdown1[commentIndex].name+"</a></li>"
            $(".tools_bar .dropdown:eq(0) ul").append(templates);
            $(".tools_bar .dropdown:eq(0) ul").on("click","li",function(){
                  if($(this).find("a").html()==dropdown1[commentIndex].name){
                    $("#modelChooerName").val(dropdown1[commentIndex].name);
                    $("#modelChooerKey").val(dropdown1[commentIndex].key);
                    // confirm("提示",$("#modelChooer").val());
                    page=0;
                    $(".form_table table  tr:not(:first)").empty(""); 
                     loadingData(page,5,dropdown1[commentIndex].key);
                    var dropdown="<span><a style='width:20px'>"+page+"</a></span>";
                    $(".navigation ul li:eq(1)").empty("");
                    $(".navigation ul li:eq(1)").append(dropdown);
                  }
                });
         });
      }});
  }
    function staticData(){
    //ajax数据交互 
      $.ajax({
              url:commonurl+"nginx/getSystem",
              type: 'GET', 
              headers: {"Authorization":ticket},
              Accept: 'application/json',
              success: function(json){
                var parameters=json;
              filePath=_.getFilePath(parameters[0].value); 
              },
              error:function(json){
                confirm("提示","系统出错路径检查不到，检查数据库路径是否正确");
              }
            });
  }
  function getUserInfo(){
      $.ajax({
              url:commonurl+"nginx/getUserRlose?username="+userName,
              type: 'GET', 
              headers: {"Authorization":ticket},
              Accept: 'application/json',
              success: function(json){
                getUserRlose=json;                
                if(getUserRlose.e!="ROLE_ADMIN"){
                    confirm("提示","非管理员账号登陆");
                    $(".add").hide();
                    $(".deleSql").hide();
                }
              },
              error:function(json){
                confirm("提示","系统出错"+ticket);
              }
            });
  }
})